home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / CMIDI 2.2 / CMIDIPort.cp < prev    next >
Encoding:
Text File  |  1994-11-30  |  6.5 KB  |  211 lines  |  [TEXT/KAHL]

  1. /*
  2.  *——— CMIDIPort.cp —————————————————————————————————————————————————————————————————————
  3.  * Copyright © Paul Ferguson, 1990-94.  All rights reserved.
  4.  *
  5.  * Superclass:  CObject
  6.  * Subclasses:  CDataPort CTimePort
  7.  *
  8.  * Description:
  9.  *    CMIDIPort.c defines a MIDI Manager port object.  CMIDIPort is an abstract type,
  10.  *    containing variables and methods common to all three port types.
  11.  *
  12.  *    For use with Symantec C++ 6.0, the accompanying THINK Class Library (TCL), and MIDI
  13.  *    Manager 2.0. Refer to the accompanying Microsoft Word document for complete
  14.  *    details about MIDI Manager objects.
  15.  *
  16.  *    If you have comments or questions about this code, you can reach me on
  17.  *    CompuServe at 70441,3055.
  18.  *
  19.  *——————————————————————————————————————————————————————————————————————————————————————
  20.  *———— NOTE ——— NOTE ——— NOTE ——— NOTE ——— NOTE ——— NOTE ——— NOTE ——— NOTE ——— NOTE ————
  21.  *——————————————————————————————————————————————————————————————————————————————————————
  22.  *    If you are not familiar with programming the Apple MIDI Manager, refer to the
  23.  *    "MIDI Management Tools" Version 2.0, available from APDA.  You MUST have the
  24.  *    software (MIDI.H and the library) from this package in order to use these objects.
  25.  *    It will not work without this.
  26.  *——————————————————————————————————————————————————————————————————————————————————————
  27.  *    REVISION HISTORY:
  28.  *        August ??, 1990            - Original release (1.0).
  29.  *        November 5, 1990        - Added checks for midiMgrVer to most methods.
  30.  *        August 1991                - updated for THINK C 5.0 as version 2.0
  31.  *        July 1993                - updated for Symantec C++ 6.0.  Some simple methods
  32.  *                                     were inlined.
  33.  *——————————————————————————————————————————————————————————————————————————————————————
  34.  */
  35.  
  36. #include "CMIDIPort.h"
  37.  
  38. extern OSType        gSignature;            // Used to register client
  39. extern CMIDIClient    * gMIDIClient;
  40.  
  41.  
  42. /*
  43.  *——— CMIDIPort::IMIDIPort —————————————————————————————————————————————————————————————
  44.  * This method is called by IMIDIInputPort(), IMIDIOutputPort(), and IMIDITimePort().
  45.  * When this method is called, the portParams structure should be completely filled in.
  46.  * 
  47.  * Note that this method checks for gMIDIClient == NULL, which might indicate that the
  48.  * gMIDIClient object was never created. This provides another check against programmer
  49.  * stupidity (present company excepted…).
  50.  *——————————————————————————————————————————————————————————————————————————————————————
  51.  */
  52.  
  53. OSErr CMIDIPort::IMIDIPort(MIDIPortParamsPtr portParams, short bufSize)
  54. {
  55.     short            theRefNum;
  56.     unsigned char *    theLen;
  57.  
  58.     if ((gMIDIClient == 0) || ((itsVersion = gMIDIClient->GetShortVerNum()) == 0))
  59.     {
  60.         itsVersion = itsPortID = itsRefNum = 0;
  61.         return (ErrNoMIDI);
  62.     }
  63.  
  64.     // Otherwise, call MIDIAddPort
  65.  
  66.     theLen = &(portParams->name[0]);        // Truncate name if necessary
  67.     if (*theLen > midiMaxNameLen) *theLen = midiMaxNameLen;
  68.  
  69.     itsPortID = portParams->portID;            // Save our port ID in the object
  70.     itsResult = MIDIAddPort(gSignature,        // Call MIDI Manager
  71.                             bufSize, &theRefNum, portParams);
  72.     itsRefNum = theRefNum;                    // Save the port reference number
  73.     return itsResult;
  74. }
  75.  
  76. /*
  77.  *——— CMIDIPort::Dispose ————————————————————————————————————————————————————
  78.  * Dispose of this MIDI port.  Note that we do NOT call MIDIRemovePort()
  79.  * here.  This has been seen to cause problems with the MIDI Manager.
  80.  *
  81.  * Normally, this isn't a problem, since you typically would only call
  82.  * this when you are quitting.  If you specifically need to call
  83.  * MIDIRemovePort(), then you can override this method.
  84.  *———————————————————————————————————————————————————————————————————————————
  85.  */
  86.  
  87. void CMIDIPort::Dispose(void)
  88. {
  89.     inherited::Dispose();
  90. }
  91.  
  92.  
  93. /*
  94.  *——— CMIDIDataPort::LoadPatches ——————————————————————————————————
  95.  * This is a virtual function which is overloaded in subclasses
  96.  * CMIDIDataPort and CMIDITimePort.
  97.  *—————————————————————————————————————————————————————————————————
  98.  */
  99.  
  100. OSErr CMIDIPort::LoadPatches(ResType theResType, short theResID)
  101. {
  102.     this->SubclassResponsibility();
  103.     return noErr;
  104. }
  105.  
  106.  
  107. /*
  108.  *——— CMIDIPort::SavePatches —————————————————————————————————————————————————
  109.  * Saves the current port connections in a specified resource.
  110.  *————————————————————————————————————————————————————————————————————————————
  111.  */
  112.  
  113. OSErr CMIDIPort::SavePatches(ResType theResType, short theResID)
  114. {
  115.     Str255                theName;
  116.     Handle                h;
  117.     MIDIPortInfoHdl        portInfo;
  118.  
  119.     if ( ! itsVersion )
  120.         return (ErrNoMIDI);
  121.  
  122.     if (itsResult != noErr)            // May have been a virtual connection,
  123.         return(itsResult);            // so don't do anything.
  124.  
  125.     GetPortName(theName);
  126.     h = GetResource(theResType, theResID);    // Delete resource if it exists
  127.     if (h)
  128.     {
  129.         RmveResource(h);
  130.         DisposHandle(h);
  131.         UpdateResFile(CurResFile());
  132.     }
  133.     portInfo = (MIDIPortInfoHdl) MIDIGetPortInfo(gSignature, itsPortID);
  134.     if (portInfo)
  135.     {
  136.         if ((**portInfo).numConnects > 0)    // No connection, no record
  137.         {
  138.             AddResource((Handle) portInfo, theResType, theResID, theName);
  139.             WriteResource((Handle) portInfo);
  140.             UpdateResFile(CurResFile());
  141.         }
  142.         ReleaseResource((Handle) portInfo);
  143.     }
  144.     return (noErr);
  145. }
  146.  
  147.  
  148. /*
  149.  *——— Misc, trivial methods ———————————————————————————————————————————
  150.  * These aren't individually commented because frankly, I didn’t feel
  151.  * like it, nor do they really need any comments besides this one.
  152.  * These are not inlined because of the infrequency of their being
  153.  * called.
  154.  *—————————————————————————————————————————————————————————————————————
  155.  */
  156.  
  157. void CMIDIPort::GetPortName(StringPtr theName)
  158. {
  159.     if (itsVersion)
  160.         MIDIGetPortName(gSignature, itsPortID, theName);
  161. }
  162.  
  163. void CMIDIPort::SetPortName(StringPtr theName)
  164. {
  165.     if (itsVersion)
  166.     {
  167.         if (theName[0] > midiMaxNameLen)
  168.             theName[0] = midiMaxNameLen;
  169.         MIDISetPortName(gSignature, itsPortID, theName);
  170.     }
  171. }
  172.  
  173. MIDIPortInfoHdl    CMIDIPort::GetPortInfo(void)
  174. {
  175.     return(itsVersion ? MIDIGetPortInfo(gSignature, itsPortID) : 0);
  176. }
  177.  
  178. void CMIDIPort::SetConnectionProc(ProcPtr theConnectProc, long theRefCon)
  179. {
  180.     if (itsVersion >= 0x200)        // Version 2.0 or later
  181.         MIDISetConnectionProc(itsRefNum, theConnectProc, theRefCon);
  182. }
  183.  
  184. void CMIDIPort::GetConnectionProc(ProcPtr * theConnectProc, long * theRefCon)
  185. {
  186.     if (itsVersion >= 0x200)        // Version 2.0 or later
  187.         MIDIGetConnectionProc(itsRefNum, theConnectProc, theRefCon);
  188. }
  189.  
  190. #ifndef __cplusplus
  191.  
  192.     // if using THINK C compiler, not Symantec C++, these are non-inline methods.
  193.  
  194.     short CMIDIPort::GetRefNum(void)
  195.     {
  196.         return itsRefNum;
  197.     }
  198.     
  199.     long CMIDIPort::GetRefCon(void)
  200.     {
  201.         return (itsVersion ? MIDIGetRefCon(itsRefNum) : 0);
  202.     }
  203.     
  204.     void CMIDIPort::SetRefCon(long theRefCon)
  205.     {
  206.         if (itsVersion) MIDISetRefCon(itsRefNum, theRefCon);
  207.     }
  208. #endif
  209.  
  210. // end of CMIDIPort.cp
  211.